ARG DOCKERHUB_USER_SECRET=+secrets/DOCKERHUB_USER
ARG DOCKERHUB_TOKEN_SECRET=+secrets/DOCKERHUB_TOKEN
ARG DOCKERHUB_MIRROR
ARG DOCKERHUB_MIRROR_INSECURE=false
ARG DOCKERHUB_AUTH=true
FROM ../..+earthly-integration-test-base \
    --DOCKERHUB_AUTH=$DOCKERHUB_AUTH \
    --DOCKERHUB_USER_SECRET=$DOCKERHUB_USER_SECRET \
    --DOCKERHUB_TOKEN_SECRET=$DOCKERHUB_TOKEN_SECRET \
    --DOCKERHUB_MIRROR=$DOCKERHUB_MIRROR \
    --DOCKERHUB_MIRROR_INSECURE=$DOCKERHUB_MIRROR_INSECURE

IMPORT .. AS tests

WORKDIR /test

test-single-line:
    DO +RUN_EARTHLY_ARGS --earthfile=single-line.earth --target=+test

test-single-line-with-args:
    DO +RUN_EARTHLY_ARGS --earthfile=single-line-with-args.earth --target=+test

test-single-line-with-comment:
    DO +RUN_EARTHLY_ARGS --earthfile=single-line-with-comment.earth --target=+test

test-multi-line:
    DO +RUN_EARTHLY_ARGS --earthfile=multi-line.earth --target=+test

test-multi-line-with-comment:
    DO +RUN_EARTHLY_ARGS --earthfile=multi-line-with-comment.earth --target=+test

test-multi-line-with-comment2:
    DO +RUN_EARTHLY_ARGS --earthfile=multi-line-with-comment2.earth --target=+test

test-multi-line-with-comment3:
    DO +RUN_EARTHLY_ARGS --earthfile=multi-line-with-comment3.earth --target=+test

test-multi-line-with-comment4:
    DO +RUN_EARTHLY_ARGS --earthfile=multi-line-with-comment4.earth --target=+test

test-multi-line-with-args:
    DO +RUN_EARTHLY_ARGS --earthfile=multi-line-with-args.earth --target=+test

test-multi-line-with-args2:
    DO +RUN_EARTHLY_ARGS --earthfile=multi-line-with-args2.earth --target=+test

test-multi-line-with-newline:
    DO +RUN_EARTHLY_ARGS --earthfile=multi-line-with-empty-newline.earth --target=+test

test-version-only-import:
    RUN mkdir subdir
    RUN echo "VERSION 0.5" > subdir/Earthfile
   DO +RUN_EARTHLY_ARGS --earthfile=version-only-import.earth --target=+test

test-version-only:
    DO +RUN_EARTHLY_ARGS --earthfile=version-only.earth --target=+base
    RUN test "$(cat Earthfile | wc -l)" = "0"

test-comment-and-whitespace-before-version:
    DO +RUN_EARTHLY_ARGS --earthfile=comment-and-whitespace-before-version.earth --target=+test

test-whitespace-then-version:
    DO +RUN_EARTHLY_ARGS --earthfile=whitespace-then-version.earth --target=+test

test-invalid-versions:
    DO +RUN_EARTHLY_ARGS --should_fail=true --earthfile=invalid-major-version.earth --target=+base
    DO +RUN_EARTHLY_ARGS --should_fail=true --earthfile=invalid-minor-version.earth --target=+base
    DO +RUN_EARTHLY_ARGS --should_fail=true --earthfile=invalid-patch-version.earth --target=+base
    DO +RUN_EARTHLY_ARGS --should_fail=true --earthfile=invalid-format-version.earth --target=+base

test-all:
    BUILD +test-single-line
    BUILD +test-single-line-with-args
    BUILD +test-single-line-with-comment
    BUILD +test-multi-line
    BUILD +test-multi-line-with-comment
    BUILD +test-multi-line-with-comment2
    BUILD +test-multi-line-with-comment3
    BUILD +test-multi-line-with-comment4
    BUILD +test-multi-line-with-args
    BUILD +test-multi-line-with-args2
    BUILD +test-multi-line-with-newline
    BUILD +test-version-only
    BUILD +test-comment-and-whitespace-before-version
    BUILD +test-whitespace-then-version
    BUILD +test-version-only-import
    BUILD +test-invalid-versions

RUN_EARTHLY_ARGS:
    COMMAND
    ARG earthfile
    ARG target
    ARG should_fail=false
    DO tests+RUN_EARTHLY \
        --earthfile=$earthfile \
        --target=$target \
        --should_fail=$should_fail \
        --DOCKERHUB_AUTH=$DOCKERHUB_AUTH \
        --DOCKERHUB_USER_SECRET=$DOCKERHUB_USER_SECRET \
        --DOCKERHUB_TOKEN_SECRET=$DOCKERHUB_TOKEN_SECRET \
        --DOCKERHUB_MIRROR=$DOCKERHUB_MIRROR \
        --DOCKERHUB_MIRROR_INSECURE=$DOCKERHUB_MIRROR_INSECURE
